import html2canvas from 'html2canvas';
export function copyObj(obj) {
    let str;
    let newobj = obj.constructor === Array ? [] : {};
    if (typeof obj !== 'object') {
        return;
    } else if (window.JSON) {
        str = JSON.stringify(obj); // 系列化对象
        newobj = JSON.parse(str); // 还原
    } else {
        for (const i in obj) {
            newobj[i] = typeof obj[i] === 'object' ? copyObj(obj[i]) : obj[i];
        }
    }
    return newobj;
}
// 下载表格
export const downloadFile = (data, title) => {
    const content = data;
    const blob = new Blob([content], { type: 'application/octet-stream' });
    const fileName = typeof data === 'string' ? title : title + '.csv';
    if (window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(blob, fileName);
    } else {
        const elink = document.createElement('a');
        elink.download = fileName;
        elink.style.display = 'none';
        elink.href =
            typeof data === 'string' ? data : URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        URL.revokeObjectURL(elink.href); // 释放URL 对象
        document.body.removeChild(elink);
    }
};
// 表单下载
export const downloadWithForm = (url, params) => {
    const form = document.createElement('form');
    form.action = window.location.origin + url;
    form.method = 'post';
    form.target = 'blank';
    Object.keys(params).forEach((item) => {
        const input = document.createElement('input');
        input.type = 'hidden';
        input.name = item;
        input.value = params[item];
        form.appendChild(input);
    });
    document.body.appendChild(form);
    form.submit();
    document.body.removeChild(form);
};
export const domToImg = (dom, name) => {
    dom.classList.add('db-img');
    const w = dom.offsetWidth;
    const h = dom.offsetHeight;
    const canvas = document.createElement('canvas');
    canvas.width = w * 3;
    canvas.height = h * 3;
    const opts = {
        useCORS: true,
        canvas: canvas,
        scale: 3,
    };
    html2canvas(dom, opts).then((canvas) => {
        const aTag = document.createElement('a');
        aTag.target = '_blank';
        aTag.href = canvas.toDataURL('image/png', 1.0);
        aTag.download = name;
        aTag.click();
        dom.classList.remove('db-img');
    });
};
export const getBrowser = () => {
    var userAgentInfo = navigator.userAgent;
    var Agents = new Array(
        'Android',
        'iPhone',
        'SymbianOS',
        'Windows Phone',
        'iPad',
        'iPod'
    );
    var flag = true;
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) {
            flag = false;
            break;
        }
    }
    return flag;
};
